const app = getApp()
const Api = require("../../../utils/api.js")
const ApiRequest = require("../../../utils/request.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    hidden: true,
    doctorFirst: "",
    doctorSecond: null,
    doctorThird: null,
    modalName: "",
    doctorName: null,
    seachList: null,
    stepChoose: [
      {id: 1, stepName: '手术流程'},
      {id: 2, stepName: '非手术流程'},
      {id: 3, stepName: '妇科介入流程'}
    ],
    templeteName: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this
    var templeteName = ""
    self.data.stepChoose.forEach(element => {
      if (element.id == options.templeteId) {
        templeteName = element.stepName
      }
    });
    console.log('传入options： ', options)
    self.setData({
      flowId: options.flowID,
      doctorId: options.doctorId,
      flowStepLength: options.flowStepLength,
      templeteName: templeteName
    })
   
    self.getDoctorList(options.flowID)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 获取医生列表
   */
  getDoctorList: function(flowID) {
    var self = this
    var url = Api.get_flow_doctor + '/' + flowID
    var params = {
      flowID: flowID
    };
    var method = 'GET';
    var header = "application/json";
    var success = function (res) {
      let data = res.data
      console.log("getDoctorList res", res)
      if(data.length == 1) {
        self.setData({
          doctorFirst: data[0],
        })
      } else if (data.length == 2) {
        self.setData({
          doctorFirst: data[0],
          doctorSecond: data[1],
        })
      } else if (data.length == 3) {
        self.setData({
          doctorFirst: data[0],
          doctorSecond: data[1],
          doctorThird: data[2]
        })
      } else {
        wx.showToast({
          icon: 'none',
          title: '获取医师信息失败',
        })
      }
    }
    ApiRequest.request(url, params, method, header, success, self.fail);
  },
  /**
   * 打开模态框选择医师
   */
  toSelect: function(e) {
    let duty = e.currentTarget.dataset.duty
    console.log(duty)
    let self = this
    self.setData({
      modalName: "choose",
      duty: duty
    })
  },
  hideInfo: function (e) {
    this.setData({
      modalName: null,
      seachList: null,
      doctorName: null
    })
  },
  chooseStep: function(e) {
    this.setData({
      modalName: 'chooseStep'
    })
  },
  hideChooseSteps: function(e) {
    this.setData({
      modalName: null,
      flowStepLength: 14
    })
  },
  getDoctorName: function(e) {
    this.setData({
      doctorName: e.detail.value,
    })
  },
  searchDoctor: function () {
    var self = this
    var url = Api.find_doctor_by_name
    var params = {
      name: self.data.doctorName
    };
    var method = 'GET';
    var header = "application/json";
    var success = function (res) {
      let data = res.data
      // console.log("find_doctor_by_name res", res)
      self.setData({
        seachList: data
      })
    }
    ApiRequest.request(url, params, method, header, success, self.fail);
  },
  appointDoctor: function(e) {
    var self = this
    let doctorInfo = e.currentTarget.dataset.info
    console.log("指定医生 ", doctorInfo)
    let id = doctorInfo.id
    let name = doctorInfo.realName
    let message = "确定指定【" + name + "】为分级医师？"
    wx.showModal({
      title: '提示',
      content: message,
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          var url = Api.appoint_doctor
          var params = {
            flowId: self.data.flowId,
            doctorId: self.data.doctorId,
            otherId: id,
            duty: self.data.duty
          };
          var method = 'POST';
          var header = "application/json";
          var success = function (res) {
            let data = res.data
            console.log("指定分级医师 res", res)
            if (self.data.duty == "二线") {
              self.setData({
                doctorSecond: data
              })
            } else {
              self.setData({
                doctorThird: data
              })
            }
            wx.showToast({
              title: '指定成功',
            })
            self.hideInfo()
          }
          ApiRequest.request(url, params, method, header, success, self.fail);
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  appointSteps: function(e) {
    var self = this
    let id = e.currentTarget.dataset.id
    var url = Api.add_steps + '/' + self.data.flowId
    var params = {
      templeteId: id,
    };
    var method = 'POST';
    var header = "application/x-www-form-urlencoded";
    var success = function (res) {
      if(res.code==200) {
        console.log("隐藏流程选定按钮")
        self.hideChooseSteps()
        wx.showModal({
          title: '提示',
          content: '成功选定流程',
          success (res) {
            // 返回上一层页面
            wx.navigateBack({ 
              delta: 1
            })
          }
        })
      }
    }
    ApiRequest.request(url, params, method, header, success, self.fail);
  },

})